package cn.chinaunicom.sdsi.frm.sys.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageInfo;

import cn.chinaunicom.sdsi.frm.base.BaseService;
import cn.chinaunicom.sdsi.frm.sys.R;
import cn.chinaunicom.sdsi.frm.sys.dao.mybatis.GacxStaffInfoMapper;
import cn.chinaunicom.sdsi.frm.sys.dao.mybatis.SysPermissionMapper;
import cn.chinaunicom.sdsi.frm.sys.dao.mybatis.SysStaffMapper;
import cn.chinaunicom.sdsi.frm.sys.entity.SysStaff;
import cn.chinaunicom.sdsi.frm.sys.entity.SysStaffOrg;
import cn.chinaunicom.sdsi.frm.sys.service.DataPermissionService;
import cn.chinaunicom.sdsi.frm.sys.service.SysStaffServiceForPolice;
import cn.chinaunicom.sdsi.frm.sys.sysinit.utils.SpringContextUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Service
public class SysStaffServiceForPoliceImp extends BaseService implements SysStaffServiceForPolice {
    private static final Log log = LogFactory.getLog(SysStaffServiceForPoliceImp.class);
    @Autowired
    private SysStaffMapper sysStaffMapper;

    private DataPermissionService sysDataPermissionService;
    @Autowired
    private SysPermissionMapper sysPermissionMapper;

    @Autowired
    private GacxStaffInfoMapper gacxStaffInfoMapper;

    // 在类实例化之前，根据配置决定注入数据权限实体类
    @PostConstruct
    public void setDP() {
        String DpServiceName = R.ProResource.getUpfVal("DP_SERVICE_NAME");
        sysDataPermissionService = (DataPermissionService) SpringContextUtil.getBean(DpServiceName);
    }

    @Override
    public JSONObject searchMap4Page(SysStaff sysStaff, Integer start, Integer pageSize) {
        SysStaff staff = getSessionCache(R.UserSign.SIGNIN_USER_SESSION);
        SysStaffOrg sysStaffOrg = getSessionCache(R.UserSign.SIGNIN_USER_STAFFORG_SESSION);
        log.debug("set search sysStaff option done");
        // 数据权限控制
        Integer topOrgId = staff.getOrgId();
        Integer staffOrgId = sysStaffOrg.getStaffOrgId();
        Integer dictid = sysPermissionMapper.selectDpType("sysStaffController/searchMap4Page");
        String sql = sysDataPermissionService.getOrgRange(staffOrgId.toString(), dictid, topOrgId);
        // 设置分页条件
        setPageInfo(start, pageSize);
        // 查询数据
        List<Map<String, Object>> staffList = sysStaffMapper.searchMap4Page(sysStaff, sql);
        log.debug("search sysStaff page done");
        PageInfo page = new PageInfo(staffList);
        JSONArray array = JSONArray.fromObject(staffList);
        JSONObject obj = new JSONObject();
        obj.element("data", array);
        obj.element("recordsTotal", page.getTotal());
        obj.element("recordsFiltered", page.getTotal());
        obj.element("draw", 0);
        log.debug("search sysStaff data to jquery datatables done");
        return obj;
    }

    @Override
    public void updateStaffLink(SysStaff sysStaff) {
    	this.gacxStaffInfoMapper.updateStaffLinkByStaff(sysStaff);
        //this.gacxStaffInfoMapper.updateStaffOrgByStaff(sysStaff);
    }

    @Override
    public void updateStaffOrg(SysStaff sysStaff) {
    	this.gacxStaffInfoMapper.updateStaffOrgByStaff(sysStaff);
        //this.gacxStaffInfoMapper.updateStaffLinkByStaff(sysStaff);
    }

    @Override
    public void addStaffLink(SysStaff sysStaff) {
        SysStaff currentUser = (SysStaff) this.getSessionCache(R.UserSign.SIGNIN_USER_SESSION);
        sysStaff.setCreateBy(currentUser.getStaffId());
        sysStaff.setCreateDate(new Date());
        this.gacxStaffInfoMapper.insertStaffLinkByStaff(sysStaff);
    }
    
    @Override
    public int selectStaffLink(Integer staffId) {
        return this.gacxStaffInfoMapper.selectStaffLinkByStaff(staffId);
    }
}
